import Define from "./common/Define";

// Learn TypeScript:
//  - [Chinese] http://docs.cocos.com/creator/manual/zh/scripting/typescript.html
//  - [English] http://www.cocos2d-x.org/docs/creator/manual/en/scripting/typescript.html
// Learn Attribute:
//  - [Chinese] http://docs.cocos.com/creator/manual/zh/scripting/reference/attributes.html
//  - [English] http://www.cocos2d-x.org/docs/creator/manual/en/scripting/reference/attributes.html
// Learn life-cycle callbacks:
//  - [Chinese] http://docs.cocos.com/creator/manual/zh/scripting/life-cycle-callbacks.html
//  - [English] http://www.cocos2d-x.org/docs/creator/manual/en/scripting/life-cycle-callbacks.html

const {ccclass, property} = cc._decorator;

@ccclass
export default class Bullet extends cc.Component {

    isValidBullet:boolean = false;
    moveSpeed:number = 1500;
    targetPos:cc.Vec2 = null;// 目标点
    newPos:cc.Vec2 = null;
    power:number = 1;
    offsetX:number = 0;

    @property(cc.Node)
    node_color:cc.Node = null;
    

    start () {

    }

    reset() {
        this.isValidBullet = true;
    }

    update (dt) {
        if(!this.isValidBullet){
            return;
        }

        let newX = this.newPos.x*this.moveSpeed*dt*6;
        let newY = this.newPos.y*this.moveSpeed*dt;

        if(Math.abs(this.node.position.x - this.targetPos.x) <= 10)
        {
            newX = 0;
        }
        this.node.position = new cc.Vec2(this.node.position.x - newX,this.node.position.y - newY);
        if(this.node.position.y >= 1200)
        {
            this.setValid(false);
        }
    }

    shoot(pos:cc.Vec2,targetPos:cc.Vec2){
        this.node.position = pos;
        this.setValid(true);
        this.targetPos = targetPos;

        let dx = this.node.position.x - this.targetPos.x;
        let dy = this.node.position.y - this.targetPos.y;

        this.newPos = new cc.Vec2(dx,dy);
        this.newPos.normalizeSelf();
    }

    setValid(isValidBullet:boolean){
        this.isValidBullet = isValidBullet;
        if(!this.isValidBullet){
            this.node.position = new cc.Vec2(-999,-999);
        }
    }
    
    setPower(power:number){
        this.power = power;
        
        if(this.power == 1)
        {
            this.node_color.color = cc.color(0,0,0);
        }else{
            let index = this.power
            if(index > 9)
            {
                index = 9
            }
            this.node_color.color = Define.ballColorRGBArr[index]
        }
    }
}
